﻿import com.dmotiko.general.*
import com.dmotiko.ada.*
import mx.transitions.easing.Regular;
import mx.transitions.Tween;

class CelebrityComponent
extends BaseClip {
	
	private var mcClose:MovieClip;
	private var mcNext:MovieClip;
	private var mcPrev:MovieClip;
	private var txtDescription:TextField;
	private var mcPhoto:MovieClip;
	private var mclPhoto:MovieClipLoader;
	private var mcLoadBar:MovieClip;
	private var txtPercent:TextField;
		
	private function initClip():Void {
		var root:CelebrityComponent = this;
		this._visible = false;
		mclPhoto = new MovieClipLoader();
		mclPhoto.addListener(this);
		mcPhoto = this.createEmptyMovieClip( "mcPhoto", this.getNextHighestDepth());
		mcPhoto._y = mcClose._y + mcClose._height + 6;
		mcPhoto._x = -2;
		mcPhoto.createEmptyMovieClip( "img", 1);
		mcPrev._visible = mcNext._visible = false;
		mcClose.onRelease = function() {
			root.registerTween( "fadeOut", new Tween( root, "_alpha", Regular.easeOut, root._alpha, 0, 0.5, true), false, true);
		}
		
		mcLoadBar._visible = txtPercent._visible = false;
	}
	
	private function tweenFinished():Void {
		this._parent.gotoAndStop(2);
	}
	
	private function refreshData():Void {
		this._visible = true;
		txtDescription.text = data.description;
		mclPhoto.loadClip( data.src, mcPhoto.img );
		mcLoadBar._visible = txtPercent._visible = true;
		mcLoadBar._xscale = 0;
		txtPercent.text = "connecting";
	}
	
	public function onLoadProgress(target:MovieClip, bytesLoaded:Number, bytesTotal:Number):Void {
		var nPercent:Number = Math.round(bytesLoaded * 100 / bytesTotal);
		if ( getTween( "barScale") ) {
			getTween( "barScale").stop();
			removeTween("barScale")
		}
		registerTween( "barScale", new Tween( mcLoadBar, "_xscale", Regular.easeOut, mcLoadBar._xscale, nPercent, 0.3, true) ); 
		txtPercent.text = nPercent+"%";
	}
	
	public function onLoadInit():Void {
		mcLoadBar._visible = txtPercent._visible = false;
		mcPhoto._alpha = 0;
		if ( this.getTween( "fadePhoto" ) ) this.getTween( "fadePhoto" ).stop();
		this.registerTween( "fadePhoto", new Tween( mcPhoto, "_alpha", Regular.easeOut, mcPhoto._alpha, 100, 0.5, true) );
	}
	
}